//
//  AssetListController.m
//  FinancialProduct
//  资产明细
//  Created by Stella on 2017/8/27.
//  Copyright © 2017年 maq. All rights reserved.
//

#import "ProductListCell.h"
#import "UIColor+NN.h"
#import "YJSegmentedControl.h"
#import "AssetListController.h"
#import "HandleNetProduct.h"
#import "InvestAssetModel.h"
#import "MJRefresh.h"
#import "UserModel.h"
#import "CKPuslichTool.h"
#import "SearchController.h"
#import "AssetAController.h"
#import "AssetBController.h"
#import "bottomLabelCell.h"
#import "InspectController.h"


@interface AssetListController ()<YJSegmentedControlDelegate>{
    
    int count;
    BOOL isBottom;
}
@property (weak, nonatomic) IBOutlet UITableView *mTableView;
@property(strong,nonatomic)NSMutableArray *dataArray;
@property(strong,nonatomic)NSString *ProductType;
@property(strong,nonatomic)NSString *ProductName;
@property(strong,nonatomic)NSString *moneyTypes;
@property(strong,nonatomic)NSString *type;

@property (weak, nonatomic) IBOutlet UIView *noDataView;
@property (weak, nonatomic) IBOutlet UIButton *LeftBt;
@property (weak, nonatomic) IBOutlet UILabel *LeftLabel;
@property (weak, nonatomic) IBOutlet UIButton *RightBt;
@property (weak, nonatomic) IBOutlet UILabel *RightLabel;
@end

@implementation AssetListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"资产明细";
    isBottom=NO;
    self.noDataView.hidden=YES;
    self.mTableView.estimatedRowHeight=180;
    self.mTableView.rowHeight = UITableViewAutomaticDimension;
    
    self.dataArray=[[NSMutableArray alloc] init];
    _type=@"存续";
    count=1;
    _ProductType=@"";
    _ProductName=@"";
    _moneyTypes=_typeStr;
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    [rightBtn setImage:[UIImage imageNamed:@"sousuoW"] forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [rightBtn addTarget:self action:@selector(rightButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:rightBtn]];
//    [self initmyButton];
  
    
    _mTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        count=1;
        [self getInvestAssetListData];
    }];
    _mTableView.mj_footer =[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        count++;
        [self getInvestAssetListData];
    }];
    
    // 马上进入刷新状态
//    [self.mTableView.mj_header beginRefreshing];
    
    count=1;
    [self getInvestAssetListData];
    // Do any additional setup after loading the view.
}
- (IBAction)ChangeStatus:(UIButton *)sender {
    self.noDataView.hidden=YES;
    isBottom=NO;
    if (sender.tag==0) {
        self.LeftBt.selected=YES;
        self.RightBt.selected=NO;
        self.LeftLabel.backgroundColor=[UIColor colorWithHexString:@"BB3535"];
        self.RightLabel.backgroundColor=[UIColor groupTableViewBackgroundColor];
        _type=@"存续";
    }else{
        self.LeftBt.selected=NO;
        self.RightBt.selected=YES;
        self.RightLabel.backgroundColor=[UIColor colorWithHexString:@"BB3535"];
        self.LeftLabel.backgroundColor=[UIColor groupTableViewBackgroundColor];
        _type=@"清算";
    }

    
    [self reloadlist];
}
-(void)reloadlist{
    [self.dataArray removeAllObjects];
    [self.mTableView reloadData];
    count=1;
    [self getInvestAssetListData];
}
/*
 * CustomerID  int
 * PageIndex   int
 * PageSize    int
 * ContractStatus  列表显示状态:  存续,到期
 * CurrencyType 币别 ：RMB , USD。默认的币别是RMB
 * ProductTypeLevel 产品分类 页面传参方式定为：现金管理传A，固收/类固收传B,一级市场传C，二级市场传D
 * ProductName 产品名字 ：模糊查询的时候使用。（非必须）
 
 
 二级市场测试客户id：107     固收/类固收测试客户id：1037
 */
-(void)getInvestAssetListData{
    if (isBottom&&count>1) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
        return;
    }
    UserModel *model=[UserModel getUser];
    int mid=[model.CustomerID intValue];
    /**现金管理传A，固收/类固收传B,一级市场传C，二级市场传D
     ProductPeriodShow**/
    NSDictionary *params=@{@"AAAMethodName":@"InvestAssetList",@"CustomerID":@(mid),
                           @"PageIndex":@(count),
                           @"PageSize":@(20),
                           @"ContractStatus":_type,
                           @"CurrencyType":_moneyTypes,
                           @"ProductTypeLevel":self.ProductType,
                           @"ProductName":self.ProductName};
//    NSLog(@"params===%@",params);
    [HandleNetProduct getListforParams:params Success:^(NSArray *data) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
        if (count==1) {
            [self.dataArray removeAllObjects];
        }
        if (data.count>0) {
            
            [self.dataArray addObjectsFromArray:data];
            isBottom=NO;
        }else{
            isBottom=YES;
        }
        if (self.dataArray.count==0) {
            self.noDataView.hidden=NO;
            
        }else{
            self.noDataView.hidden=YES;
        }
        [self.mTableView reloadData];
    } Failure:^(NSString *errorInfo) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
    }];
   
}
-(void)rightButton:(UIBarButtonItem *)barBt{
    //去搜索页面
    SearchController *vc=(SearchController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"SearchController"];
    vc.returnSearchContent = ^(NSString *searchContent) {
        self.ProductName=searchContent;
        self.ProductType=@"";
        _moneyTypes=_typeStr;
        self.noDataView.hidden=YES;
       
        [self reloadlist];
    };
    vc.returnCategory = ^(NSUInteger index) {
        switch (index) {
            case 0:
                self.ProductType=@"";
                break;
            case 1:
                self.ProductType=@"A";
                break;
            case 2:
                self.ProductType=@"B";
                break;
            case 3:
                self.ProductType=@"C";
                break;
            case 4:
                self.ProductType=@"D";
                break;
                
            default:
                break;
        }
        _moneyTypes=_typeStr;
        self.ProductName=@"";
        self.noDataView.hidden=YES;
        
        [self reloadlist];
    };
    vc.returnMoneyType = ^(NSString *moneyType) {
        if ([moneyType isEqualToString:@"人民币"]) {
            self.moneyTypes=@"RMB";
            
        }else{
            self.moneyTypes=@"USD";
        }
        self.ProductType=@"";
        self.ProductName=@"";
        self.noDataView.hidden=YES;
        
        [self reloadlist];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
/*
-(void)initmyButton{
    [self.segment removeFromSuperview];
    NSArray *btnDataSource=@[@"存续资产",@"到期资产"];
    //  第4部  调用创建
    YJSegmentedControl * segment = [YJSegmentedControl segmentedControlFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50) titleDataSource:btnDataSource backgroundColor:[UIColor whiteColor] titleColor:[UIColor colorWithHexString:@"#666666"] titleFont:[UIFont systemFontOfSize:17] selectColor:[UIColor colorWithHexString:@"BB3535"] buttonDownColor:[UIColor colorWithHexString:@"BB3535"] Delegate:self];
    // 第5步 添加到试图上
    self.segment = segment;
    self.segment.delegate=self;
    [self.view insertSubview:self.segment atIndex:1];
}
#pragma mark  选择点击segumentSelectionChange

//是否使用   -1 已过期，0 未使用   1 已使用
-(void)segumentSelectionChange:(NSInteger)selection{
    
    if (selection==0) {
        //NSLog(@"存续资产");
        type=1;
    }else{
        //NSLog(@"到期资产");
        type=2;
    }
    [self.mTableView.mj_header beginRefreshing];
}

*/
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//#pragma mark - Table view data source
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 180;
//    
//}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (isBottom) {
        return self.dataArray.count+1;
    }
    return self.dataArray.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
 *产品分类。页面传参方式定为：现金管理传A，固收/类固收传B,一级市场传C，二级市场传D
 *
 */
    if (indexPath.section==self.dataArray.count) {
        bottomLabelCell *cell=[tableView dequeueReusableCellWithIdentifier:@"bottomLabelCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryNone;
        cell.backgroundColor=[UIColor clearColor];
        cell.totalLabel.text=[NSString stringWithFormat:@"已到底部，共加载%d条信息",(int)self.dataArray.count];
        return cell;
    }
    
    NSDictionary *dic=self.dataArray[indexPath.section];
    //NSLog(@"Templet:::::%@",dic[@"Templet"]);
    if ([dic[@"ProductTypeLevel1"] isEqualToString:@"现金管理类"]||[dic[@"ProductTypeLevel1"] isEqualToString:@"固收&类固收"]) {
        ProductListCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ProductListCellA" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryNone;
        InvestAssetModel *gushouMode=[InvestAssetModel mj_objectWithKeyValues:dic];
        cell.setModel=gushouMode;
         [cell layoutIfNeeded];
        cell.rightBtn.tag = indexPath.section;
        [cell.rightBtn addTarget:self action:@selector(clickRightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }else if ([dic[@"ProductTypeLevel1"] isEqualToString:@"一级市场"]) {
        ProductListCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ProductListCellC" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryNone;
//        FirstAssetModel *mMode=[FirstAssetModel mj_objectWithKeyValues:dic];
//        NSLog(@"IMSProductName--%@",mMode.IMSProductName);
//        NSLog(@"Templet---%@",dic[@"Templet"]);
//        cell.firstModel=mMode;
        cell.firstdic = dic;
        [cell layoutIfNeeded];
        cell.rightBtn.tag = indexPath.section;
        [cell.rightBtn addTarget:self action:@selector(clickRightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }else if ([dic[@"ProductTypeLevel1"] isEqualToString:@"二级市场"]) {
        ProductListCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ProductListCellD" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryNone;
        SecondAssetModel *secMode=[SecondAssetModel mj_objectWithKeyValues:dic];
        cell.secondModel=secMode;
        [cell layoutIfNeeded];
        cell.rightBtn.tag = indexPath.section;
        [cell.rightBtn addTarget:self action:@selector(clickRightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }else{
        ProductListCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ProductListCellB" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryNone;
//        InvestAssetModel *gushouMode=[InvestAssetModel mj_objectWithKeyValues:dic];
//        cell.setModel=gushouMode;
        InvestMangeModel *mode=[InvestMangeModel mj_objectWithKeyValues:dic];
        cell.mangeModel=mode;
        [cell layoutIfNeeded];
        
        cell.rightBtn.tag = indexPath.section;
        [cell.rightBtn addTarget:self action:@selector(clickRightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic=self.dataArray[indexPath.section];
    if ([dic[@"Templet"] isEqualToString:@"模板一"]) {//固收&类固收
        AssetAController *vc=(AssetAController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"AssetAController"];
        vc.infoDic=dic;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        AssetBController *vc=(AssetBController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"AssetBController"];
        vc.infoDic=dic;
        [self.navigationController pushViewController:vc animated:YES];
   
    }
}

- (void)clickRightBtnAction:(UIButton *)sender {
    NSDictionary *dic=self.dataArray[sender.tag];
    InspectController *vc=(InspectController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"InspectController"];
    vc.productId = dic[@"ProductID"];
    vc.customerID = dic[@"CustomerID"];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
